﻿' Frowser - http://www.frowser.net
' Copyright (c) 2010
' by Will Morgenweck, Active Modules, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
Imports System.Reflection
Namespace Frowser.Utilities
    Public Class JSON
        Public Shared Function Pair(ByVal name As String, ByVal value As String) As String
            Return Pair(name, value, False)
        End Function
        Public Shared Function Pair(ByVal name As String, ByVal value As String, ByVal isObject As Boolean) As String
            If String.IsNullOrEmpty(value) Then
                value = Chr(34) & Chr(34)
            ElseIf Not IsNumeric(value) And Not (value.ToLower = "true" Or value.ToLower = "false") And isObject = False Then
                value = Chr(34) & JSON.EscapeJsonString(value) & Chr(34)
            ElseIf value.Trim.ToLowerInvariant = "true" Or value.Trim.ToLowerInvariant = "false" Then
                value = value.Trim.ToLower
            End If
            Return Chr(34) & name & Chr(34) & ":" & value
        End Function
        Public Shared Function IsJSONArray(ByVal test As String) As Boolean
            Return test.StartsWith("{") AndAlso Not test.StartsWith("{*") OrElse test.StartsWith("[")
        End Function
        ''' <summary>
        ''' Escape backslashes and double quotes
        ''' </summary>
        ''' <param name="originalString">string</param>
        ''' <returns>string</returns>
        Friend Shared Function EscapeJsonString(ByVal originalString As String) As String
            originalString = originalString.Replace(ControlChars.NewLine, "\n")
            Dim reg As New Regex("\s+", RegexOptions.Multiline)
            originalString = reg.Replace(originalString, " ")
            Return If(IsJSONArray(originalString), originalString, originalString.Replace("\/", "/").Replace("/", "\/").Replace("\""", """").Replace("""", "\""").Replace(vbNewLine, " "))
        End Function
    End Class

End Namespace
